<template>
  <j-modal
    :width="modalWidth"
    :visible="visible"
    :title="title"
    switchFullscreen
    @ok="handleSubmit"
    @cancel="close"
    style="top: 50px"
    cancelText="关闭"
  >
    <a-card :bordered="false">
<!--      &lt;!&ndash; 查询区域 &ndash;&gt;-->
<!--      <div class="table-page-search-wrapper">-->
<!--        <a-form layout="inline" @keyup.enter.native="searchQuery">-->
<!--          <a-row :gutter="24">-->
<!--            <a-col :xl="10" :lg="11" :md="12" :sm="24">-->
<!--              <a-form-item label="开始日期">-->
<!--                <j-date placeholder="请选择开始日期" class="query-group-cust" v-model="startTime_begin"></j-date>-->
<!--                <span class="query-group-split-cust"></span>-->
<!--                <j-date placeholder="请选择结束日期" class="query-group-cust" v-model="startTime_end"></j-date>-->
<!--              </a-form-item>-->
<!--            </a-col>-->
<!--            <a-col :xl="6" :lg="7" :md="8" :sm="24">-->
<!--              <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">-->
<!--                <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>-->
<!--                <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>-->
<!--                &lt;!&ndash;              <a @click="handleToggleSearch" style="margin-left: 8px">&ndash;&gt;-->
<!--                &lt;!&ndash;                {{ toggleSearchStatus ? '收起' : '展开' }}&ndash;&gt;-->
<!--                &lt;!&ndash;                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>&ndash;&gt;-->
<!--                &lt;!&ndash;              </a>&ndash;&gt;-->
<!--              </span>-->
<!--            </a-col>-->
<!--          </a-row>-->
<!--        </a-form>-->
<!--      </div>-->
      <!-- 查询区域-END -->
      <!-- table区域-begin -->
      <div>
<!--        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">-->
<!--          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择-->
<!--          <a style="font-weight: 600">{{ selectedRowKeys.length }}</a-->
<!--          >项-->
<!--          <a style="margin-left: 24px" @click="onClearSelected">清空</a>-->
<!--        </div>-->

        <a-tabs v-model="activeKey" @change="handleChangeTab">
          <a-tab-pane key="materialOrder" tab="原材料采购订单">
            <a-table
              ref="table"
              size="middle"
              bordered
              :rowKey="
              (record, index) => {
                return record.detailId
              }
            "
              class="j-table-force-nowrap1"
              :scroll="{ x: true }"
              :columns="columns"
              :dataSource="materialOrderTable.dataSource"
              :pagination="materialOrderTable.ipagination"
              :loading="materialOrderTable.loading"
              :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: checkval }"
              @change="handleTableChange"
              :orderRow="orderClick"
            >
            </a-table>
          </a-tab-pane>
          <a-tab-pane key="sparePartOrder" tab="五金配件采购订单">
            <a-table
              ref="table"
              size="middle"
              bordered
              :rowKey="
              (record, index) => {
                return record.detailId
              }
            "
              class="j-table-force-nowrap1"
              :scroll="{ x: true }"
              :columns="columns"
              :dataSource="sparePartOrderTable.dataSource"
              :pagination="sparePartOrderTable.ipagination"
              :loading="sparePartOrderTable.loading"
              :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: checkval }"
              @change="handleTableChange"
              :orderRow="orderClick"
            >
            </a-table>
          </a-tab-pane>
          <a-tab-pane key="packagingMaterialsOrder" tab="包材采购订单">
            <a-table
              ref="table"
              size="middle"
              bordered
              :rowKey="
              (record, index) => {
                return record.detailId
              }
            "
              class="j-table-force-nowrap1"
              :scroll="{ x: true }"
              :columns="columns"
              :dataSource="packagingMaterialsOrderTable.dataSource"
              :pagination="packagingMaterialsOrderTable.ipagination"
              :loading="packagingMaterialsOrderTable.loading"
              :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: checkval }"
              @change="handleTableChange"
              :orderRow="orderClick"
            >
            </a-table>
          </a-tab-pane>
          <a-tab-pane key="promotionalItemsOrder" tab="促销品采购订单">
            <a-table
              ref="table"
              size="middle"
              bordered
              :rowKey="
              (record, index) => {
                return record.detailId
              }
            "
              class="j-table-force-nowrap1"
              :scroll="{ x: true }"
              :columns="columns"
              :dataSource="promotionalItemsOrderTable.dataSource"
              :pagination="promotionalItemsOrderTable.ipagination"
              :loading="promotionalItemsOrderTable.loading"
              :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: checkval }"
              @change="handleTableChange"
              :orderRow="orderClick"
            >
            </a-table>
          </a-tab-pane>
        </a-tabs>
      </div>
    </a-card>
  </j-modal>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import '@/assets/less/TableExpand.less'
import moment from 'dayjs'
import ActApplyBtn from '@views/flowable/components/ActApplyBtn.vue'
import { getAction } from '@api/manage'

export default {
  name: 'SelectOrderListModal',
  mixins: [JeecgListMixin],
  components: { ActApplyBtn },
  props: {
    modalWidth: {
      type: Number,
      required: false,
      default: 1250,
    },
    checkval: {
      type: String,
      default: 'radio',
    },
    title: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      description: '采购订单选择',
      applications: {},
      activeKey: 'materialOrder',
      visible: false,
      queryParam: {
        instant: 999,
        openState: 'Y',
      },
      dateFormat: 'YYYY-MM-DD',
      materialOrderTable: {
        loading: false,
        dataSource: [],
        // 表头
        ipagination: {
          current: 1,
          pageSize: 30,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        }
      },
      sparePartOrderTable:{
        loading: false,
        dataSource: [],
        // 表头
        ipagination: {
          current: 1,
          pageSize: 30,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        }
      },
      packagingMaterialsOrderTable:{
        loading: false,
        dataSource: [],
        // 表头
        ipagination: {
          current: 1,
          pageSize: 30,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        }
      },
      promotionalItemsOrderTable:{
        loading: false,
        dataSource: [],
        // 表头
        ipagination: {
          current: 1,
          pageSize: 30,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        }
      },
      moment,
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '单据编号',
          align: 'center',
          dataIndex: 'code',
        },
        {
          title: '物料编号',
          align: 'center',
          dataIndex: 'materialCode',
        },
        {
          title: '购货名称',
          align: 'center',
          dataIndex: 'materialName',
        },
        {
          title: '物料规格',
          align: 'center',
          dataIndex: 'spec',
        }
      ],
      url: {
        materialOrderList: '/business/bussMaterialProcurement/list',
        packageOrderList: '/package/orderManage/list'
      },
      dictOptions: {},
      superFieldList: []
    }
  },
  created() {
    this.$forceUpdate()
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    handleChangeTab() {
      switch (this.activeKey) {
        case 'materialOrder':
          this.loadMaterialData()
          break
        case 'sparePartOrder':
          this.loadSparePartData()
          break
        case 'packagingMaterialsOrder':
          this.loadPackagingMaterialsData()
          break
        case 'promotionalItemsOrder':
          this.loadPromotionalItemsData()
          break
        default:
          break
      }
    },
    loadMaterialData(){
      //已审核单据，status=999
      this.tabIndex = 1
      this.queryParam.openState = ''
      let para = {
        ...this.queryParam,
        status: 999,
        sourceFrom: 1
      }
      this.materialOrderTable.loading = true
      getAction(this.url.materialOrderList, para)
        .then((res) => {
          if (res.success) {
            this.materialOrderTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.materialOrderTable.ipagination.total = res.result.total
            } else {
              this.materialOrderTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.materialOrderTable.loading = false
        })
    },
    loadSparePartData(){
      //已审核单据，status=999
      this.tabIndex = 2
      this.queryParam.openState = ''
      let para = {
        ...this.queryParam,
        status: 999,
        sourceFrom: 2
      }
      this.sparePartOrderTable.loading = true
      getAction(this.url.materialOrderList, para)
        .then((res) => {
          if (res.success) {
            this.sparePartOrderTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.sparePartOrderTable.ipagination.total = res.result.total
            } else {
              this.sparePartOrderTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.sparePartOrderTable.loading = false
        })
    },
    loadPackagingMaterialsData(){
      //已审核单据，status=999
      this.tabIndex = 1
      this.queryParam.openState = ''
      let para = {
        ...this.queryParam,
        status: 999
      }
      this.packagingMaterialsOrderTable.loading = true
      getAction(this.url.packageOrderList, para)
        .then((res) => {
          if (res.success) {
            this.packagingMaterialsOrderTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.packagingMaterialsOrderTable.ipagination.total = res.result.total
            } else {
              this.packagingMaterialsOrderTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.packagingMaterialsOrderTable.loading = false
        })
    },
    loadPromotionalItemsData(){
      //已审核单据，status=999
      this.tabIndex = 1
      this.queryParam.openState = ''
      let para = {
        ...this.queryParam,
        status: 999,
        sourceFrom: 3
      }
      this.promotionalItemsOrderTable.loading = true
      getAction(this.url.materialOrderList, para)
        .then((res) => {
          if (res.success) {
            this.promotionalItemsOrderTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.promotionalItemsOrderTable.ipagination.total = res.result.total
            } else {
              this.promotionalItemsOrderTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.promotionalItemsOrderTable.loading = false
        })
    },
    close() {
      this.searchReset(false)
      this.visible = false
    },
    handleSubmit() {
      this.getSelectRows()
      // this.searchReset(false)
      this.onClearSelected()
      this.close()
    },
    getSelectRows() {
      let selectedRow = null
      if (this.checkval == 'radio') {
        selectedRow = this.selectionRows[0]
      } else {
        selectedRow = this.selectionRows
      }
      //传递选中的数据
      this.$emit('selectFinished', selectedRow)
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
    },
    add() {
      this.visible = true
      this.loadData()
    },
    async loadData() {
      //分别根据单据状态查询结果并将其放在
      this.queryParam = this.getQueryParams()
      this.loadMaterialData()
    },
    initDictConfig() {},
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'code', text: '单据编号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'materialCode', text: '物料编号', dictCode: '' })
      fieldList.push({ type: 'date', value: 'materialName', text: '购货名称' })
      fieldList.push({ type: 'date', value: 'spec', text: '物料规格' })
      this.superFieldList = fieldList
    },
    orderClick(record) {
      var that = this;
      return {
        on: {
          dblclick: () => {
            that.$emit('selectFinished', record)
            that.queryParam = {}
            that.onClearSelected()
            that.close()
          },
        },
      }
    }
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
